package com.ruoyi.web.controller.signer;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SealTempjur;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISealJurSignerService;
import com.ruoyi.system.service.ISealTempjurBackService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISealTempjurService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人印章转授回收Controller
 * 
 * @author ess
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/signer/tempjurback")
public class SealTempJurBackController extends BaseController
{
    @Autowired
    private ISealTempjurBackService sealTempjurBackService;

    @Autowired
    private ISealJurSignerService sealJurSignerService;

    /**
     * 新增印章转授管理
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:add')")
    @Log(title = "个人印章转授", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SealTempjur sealTempjur)
    {

        LoginUser loginUser = getLoginUser();
        sealTempjur.setJurPersonId(loginUser.getUser().getPersonId());


        if (StringUtils.isEmpty(sealTempjur.getUseJurPersonId())) {
            return AjaxResult.error("转授失败，转授人不能为空，请搜索添加");
        }


//        判断是否已经转授过
        if (!sealTempjurBackService.checkAreadyTempJur(sealTempjur)) {
         return AjaxResult.error("转授失败，已对此人转授过此授权");
        }

//        判断到期时间 和次数 是否合规
        if (!sealJurSignerService.checkDueTime(sealTempjur.getDueTime(),sealTempjur.getSealJurId())) {
        return AjaxResult.error("转授失败，原授权过期或转授到期时间错误");
       }
        if ( !sealJurSignerService.checkUseCount(sealTempjur.getUseCountTemp(),sealTempjur.getSealJurId())) {
            return AjaxResult.error("转授失败，转授次数错误");
        }

        return toAjax(sealTempjurBackService.insertSealTempjur(sealTempjur));
    }


    /**
     * 查询个人印章转授管理列表
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:list')")
    @GetMapping("/list")
    public TableDataInfo list(SealTempjur sealTempjur)
    {
        LoginUser loginUser = getLoginUser();
        sealTempjur.setJurPersonId(loginUser.getUser().getPersonId());
        startPage();
        List<SealTempjur> list = sealTempjurBackService.selectSealTempjurList(sealTempjur);
        return getDataTable(list);
    }

    /**
     * 导出个人印章转授管理列表
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:export')")
    @Log(title = "印章转授回收", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SealTempjur sealTempjur)
    {
        LoginUser loginUser = getLoginUser();
        sealTempjur.setJurPersonId(loginUser.getUser().getPersonId());
        List<SealTempjur> list = sealTempjurBackService.selectSealTempjurList(sealTempjur);
        ExcelUtil<SealTempjur> util = new ExcelUtil<SealTempjur>(SealTempjur.class);
        return util.exportExcel(list, "印章转授管理数据");
    }

    /**
     * 获取个人印章转授管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:query')")
    @GetMapping(value = "/{tempJurId}")
    public AjaxResult getInfo(@PathVariable("tempJurId") String tempJurId)
    {
        return AjaxResult.success(sealTempjurBackService.selectSealTempjurByTempJurId(tempJurId));
    }


    /**
     * 修改个人印章转授管理
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:edit')")
    @Log(title = "个人印章转授回收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SealTempjur sealTempjur)
    {
//        校验到期时间和使用次数

        return toAjax(sealTempjurBackService.updateSealTempjur(sealTempjur));
    }

    /**
     * 删除印章转授管理
     */
    @PreAuthorize("@ss.hasPermi('signer:tempjurback:remove')")
    @Log(title = "个人印章转授回收", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tempJurIds}")
    public AjaxResult remove(@PathVariable String[] tempJurIds)
    {
        return toAjax(sealTempjurBackService.deleteSealTempjurByTempJurIds(tempJurIds));
    }
}
